﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine.Modelo;
using PloobsEngine.SceneControl;
using PloobsEngine.Cameras;


namespace PloobsEngine.Material
{
    public class BumpSpecularDeferred : IShader
    {
        private string effect;
        private Effect _shader;
        
        /// <summary>
        /// Usa o local Default "..\\Content\\Effects\\Deferred\\BumpGBuffer"
        /// </summary>
        public BumpSpecularDeferred ()
        {
            this.effect = "SpecularBumpBuffer";
        }

        public override MaterialType MaterialType
        {
            get { return MaterialType.DEFERRED; }
        }

        public override void Draw(IModelo modelo,IRenderHelper render, ICamera cam)
        {
            if (obj != null)
            {
                this._shader.Parameters["id"].SetValue(shaderId);         
                this._shader.Parameters["View"].SetValue(cam.View);
                this._shader.Parameters["Projection"].SetValue(cam.Projection);
                this._shader.Parameters["Texture"].SetValue(modelo.getTexture(TextureType.DIFFUSE));
                this._shader.Parameters["NormalMap"].SetValue(modelo.getTexture(TextureType.BUMP));
                this._shader.Parameters["SpecularMap"].SetValue(modelo.getTexture(TextureType.SPECULAR));

                Matrix[] boneTransforms = modelo.getBonesTransformation();
                Matrix wld = obj.getWorldMatrix();
                for (int i = 0; i < modelo.MeshNumber; i++)
                {
                    BatchInformation[] bi = modelo.GetBatchInformation(i);
                    Matrix w1 = Matrix.Multiply(wld, boneTransforms[modelo.GetParentBoneIndex(i)]);
                    this._shader.Parameters["World"].SetValue(w1);                        
                    for (int j = 0; j < bi.Count(); j++)
                    {

                        this._shader.Begin();
                        EngineStuff.GraphicsDevice.VertexDeclaration = bi[j].VertexDeclaration;
                        EngineStuff.GraphicsDevice.Vertices[0].SetSource(modelo.GetVertexBuffer(i), bi[j].StreamOffset, bi[j].VertexStride);
                        EngineStuff.GraphicsDevice.Indices = modelo.GetIndexBuffer(i);

                        this._shader.CurrentTechnique.Passes[0].Begin();
                        EngineStuff.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, bi[j].BaseVertex, 0, bi[j].NumVertices, bi[j].StartIndex, bi[j].PrimitiveCount);
                        this._shader.CurrentTechnique.Passes[0].End();
                        this._shader.End();
                    }

                }
            }
        }
        public override void Initialize()
        {
            base.Initialize();
            this._shader = EngineStuff.InternalContentManager.GetAsset<Effect>(effect);            
        }
    }
}
